package com.example.sqlexport.controller;

import com.example.sqlexport.model.ApiResponse;
import com.example.sqlexport.model.request.TestConnectionRequest;
import com.example.sqlexport.entity.DatabaseConfig;
import com.example.sqlexport.service.DatabaseConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/database-config")
public class DatabaseConfigController {

    @Autowired
    private DatabaseConfigService databaseConfigService;

    @GetMapping
    public ApiResponse<List<DatabaseConfig>> findAll() {
        try {
            List<DatabaseConfig> configs = databaseConfigService.findAll();
            return ApiResponse.success(configs);
        } catch (Exception e) {
            return ApiResponse.error("获取数据库配置列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/{id}")
    public ApiResponse<DatabaseConfig> findById(@PathVariable Long id) {
        try {
            DatabaseConfig config = databaseConfigService.findById(id);
            if (config != null) {
                return ApiResponse.success(config);
            } else {
                return ApiResponse.error(404, "数据库配置未找到: " + id);
            }
        } catch (Exception e) {
            return ApiResponse.error("获取数据库配置失败: " + e.getMessage());
        }
    }

    @PostMapping
    public ApiResponse<DatabaseConfig> save(@RequestBody DatabaseConfig config) {
        try {
            databaseConfigService.save(config);
            return ApiResponse.success(config, "数据库配置添加成功");
        } catch (Exception e) {
            return ApiResponse.error("添加数据库配置失败: " + e.getMessage());
        }
    }

    @PutMapping("/{id}")
    public ApiResponse<DatabaseConfig> update(@PathVariable Long id, @RequestBody DatabaseConfig config) {
        try {
            config.setId(id);
            databaseConfigService.update(config);
            return ApiResponse.success(config, "数据库配置更新成功");
        } catch (Exception e) {
            return ApiResponse.error("更新数据库配置失败: " + e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public ApiResponse<Void> delete(@PathVariable Long id) {
        try {
            databaseConfigService.delete(id);
            return ApiResponse.success(null, "数据库配置删除成功");
        } catch (Exception e) {
            return ApiResponse.error("删除数据库配置失败: " + e.getMessage());
        }
    }

    @PostMapping("/test-connection")
    public ApiResponse<Boolean> testConnection(@RequestBody TestConnectionRequest request) {
        try {
            boolean isConnected = databaseConfigService.testConnection(request);
            if (isConnected) {
                return ApiResponse.success(true, "数据库连接成功！");
            }
            return ApiResponse.error(500, "数据库连接失败，未知原因。(Service did not throw)");
        } catch (RuntimeException e) {
            return ApiResponse.error(500, e.getMessage());
        }
    }
} 